package com.bg.print.controller;

import com.bg.print.entity.MaterialEntity;
import com.bg.print.service.MaterialService;
import com.bg.print.utils.PageUtils;
import com.bg.print.utils.R;
import com.bg.print.variable.Enabled;
import com.bg.print.variable.MaterialValue;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 材料
 *
 * @author liuxvru
 * @email liuxvru@163.com
 * @date 2024-03-25 17:50:46
 */
@Tag(name = "材料")
@RestController
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    /**
     * 列表
     */
    @Operation(summary = "列表用户使用 前台")
    @GetMapping("/materials")
    public R materials(@RequestParam Map<String, Object> params) {
        PageUtils page = materialService.queryUserPage(params);
        return R.ok().put("data", page);
    }
    @Operation(summary = "列表 后台使用")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = materialService.queryPages(params);
        return R.ok().put("data", page);
    }

    /**
     * 信息
     */
    @Operation(summary = "信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MaterialEntity material = materialService.getById(id);
        return R.ok().put("data", material);
    }

    /**
     * 保存
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R save(@RequestBody MaterialEntity material) {
        material.setEnabled(Enabled.USING.getCode());
        material.setIsListed(MaterialValue.isListed.UNDERCARRIAGE.getCode());
        material.setStockStatus(MaterialValue.stockStatus.OUT_OF_STOCK.getCode());
        materialService.save(material);
        return R.ok();
    }

    /**
     * 修改
     */
    @Operation(summary = "修改(不会修改上架信息和有无货信息)")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R update(@RequestBody MaterialEntity material) {
        material.setIsListed(null);
        material.setStockStatus(null);
        materialService.updateById(material);
        return R.ok();
    }

    @Operation(summary = "上架")
    @PutMapping("/putaway/{id}")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R putaway(@PathVariable("id") Long id) {
        Integer ok = materialService.updataIsListed(
                id,
                MaterialValue.isListed.UNDERCARRIAGE.getCode(),
                MaterialValue.isListed.GROUNDING.getCode()
        );
        if (ok != 0)
            return R.ok();
        return R.error(500, "上架失败");
    }
    @Operation(summary = "下架")
    @PutMapping("/undercarriage/{id}")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R undercarriage(@PathVariable("id") Long id) {
        Integer ok = materialService.updataIsListed(
                id,
                MaterialValue.isListed.GROUNDING.getCode(),
                MaterialValue.isListed.UNDERCARRIAGE.getCode()
        );
        if (ok != 0)
            return R.ok();
        return R.error(500, "下架失败");
    }

    @Operation(summary = "有货")
    @PutMapping("/toavailable/{id}")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R toAvailable(@PathVariable("id") Long id) {
        Integer ok = materialService.updataStockStatus(
                id,
                MaterialValue.stockStatus.OUT_OF_STOCK.getCode(),
                MaterialValue.stockStatus.AVAILABLE.getCode()
        );
        if (ok != 0)
            return R.ok();
        return R.error(500, "有货状态切换失败");
    }
    @Operation(summary = "无货")
    @PutMapping("/outofstock/{id}")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R outOfStock(@PathVariable("id") Long id) {
        Integer ok = materialService.updataStockStatus(
                id,
                MaterialValue.stockStatus.AVAILABLE.getCode(),
                MaterialValue.stockStatus.OUT_OF_STOCK.getCode()
        );
        if (ok != 0)
            return R.ok();
        return R.error(500, "无货状态切换失败失败");
    }
    /**
     * 删除
     */
    @Operation(summary = "删除")
    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R delete(@RequestBody Long[] ids) {
        materialService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


}
